//
//  NSControl+Block.m
//  BabyBluetoothOSDemo
//
//  Created by 赵梁 on 2019/9/20.
//  Copyright © 2019 liuyanwei. All rights reserved.
//

#import "NSControl+Block.h"

#import <objc/runtime.h>

static void *NSControlBlockKey = &NSControlBlockKey;

@implementation NSControl (Block)

- (void (^)(void))tapBlock
{
    return objc_getAssociatedObject(self, NSControlBlockKey);
}

- (void)setTapBlock:(void (^)(void))tapBlock
{
    if (tapBlock) {
        self.target = self;
        self.action = @selector(__tap);
    }
    
    objc_setAssociatedObject(self, NSControlBlockKey, tapBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)__tap {
    if (self.tapBlock) {
        self.tapBlock(self);
    }
}



@end
